define(['jquery', 'bootstrap', 'backend', 'table', 'form',  'upload'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'buddha/index',
                    add_url: 'buddha/add',
                    edit_url: 'buddha/edit',
                    del_url: 'buddha/del',
                    multi_url: 'buddha/multi',
                    dragsort_url: 'ajax/weigh',
                    table: 'buddha',
                },
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pagination: true,
                commonSearch: true,
                search: true,
                fixedColumns: true,         // 打开冻结按钮
                fixedNumber:  2,            // 固定左侧，固定左侧几列填几
                // fixedRightNumber: 3,     // 固定右侧，固定右侧几列填几
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID'), sortable: true},
                        {field: 'name', title: __('Name'), sortable: true, formatter: Controller.api.formatter.filename, operate: 'like'},
                        {field: 'img', title: __('Url'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},      
                        {field: 'start_num', title: __('Buddha StartNo'), sortable: true},                    
                        {field: 'end_num', title: __('Buddha EndNo'), sortable: true},      
                        // 是否默认图片
                        // {field: 'is_default', title: __('IsDefault'), sortable: true, operate: false, formatter:function (value, row, index) {                                    
                        //         return value == 1 ? '<span class="fa fa-check" style="color: green"></span>' : '<span class="fa fa-close" style="color: red;"></span>';
                        //     }
                        // },
                        // 是否生效
                        {field: 'status', title: __('Status'),sortable: true,  operate: false, formatter:function (value, row, index) {
                                
                                return value == 1 ? '<span class="text-success label label-success">生效</span>' : '<span class="text-gray label label-default">无效</span>';
                            }
                        },
                        {field: 'sort_order', title: __('Sort'), sortable: true},   
                        {
                            field: 'create_time',
                            title: __('Createtime'),
                            sortable: true,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        // 预览效果
                        // {field: 'view_url', title: __('View'), align: 'center', formatter:function (value, row, index) {
                        //         var html = `<div class="input-group input-group-sm">
                        //                     <span class="input-group-btn input-group-sm">
                        //                         <a href="/` + Config.frontend_module + `/ebook/download/id/${row.id}.html" target="_blank" class="btn btn-default btn-sm">
                        //                             <i class="fa fa-link"></i>${__('Preview')}
                        //                         </a>
                        //                     </span>
                        //                 </div>`;
                        //         return html;
                        //     }
                        // },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'edit',
                                    text: __('Edit'),
                                    title: __('Edit'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-edit',
                                    extend: 'data-area=\'["90%","95%"]\'',
                                    url: 'buddha/edit',
                                    callback: function(data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), { title: "回传数据" });
                                    },
                                    visible: function(row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                }
                            ]
                        }
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            
            // 假设你已经初始化了BootstrapTable，并且你的表格ID为#table
            $("#table").on("click", ".btn-editone", function(){
                // 获取当前行数据
                var row = $(this).closest('tr[data-index]');
                var rowData = $("#table").bootstrapTable('getData')[row.data('index')];
                window.location.href = '/goods/edit/' + rowData.id;
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "#c-type", function () {
                    $("#c-pid option[data-type='all']").prop("selected", true);
                    $("#c-pid option").removeClass("hide");
                    $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
                });
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                // thumb: function (value, row, index) {
                //     var html = '';
                //     if (row.mimetype.indexOf("image") > -1) {
                //         html = '<a href="' + row.fullurl + '" target="_blank"><img src="' + row.fullurl + row.thumb_style + '" alt="" style="max-height:60px;max-width:120px"></a>';
                //     } else {
                //         html = '<a href="' + row.fullurl + '" target="_blank"><img src="' + Fast.api.fixurl("ajax/icon") + "?suffix=" + row.imagetype + '" alt="" style="max-height:90px;max-width:120px"></a>';
                //     }
                //     return '<div style="width:120px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + html + '</div>';
                // },
                // url: function (value, row, index) {
                //     return '<a href="' + row.fullurl + '" target="_blank" class="label bg-green">' + row.url + '</a>';
                // },
                filename: function (value, row, index) {
                    return '<div style="width:150px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + Table.api.formatter.search.call(this, value, row, index) + '</div>';
                },
                // mimetype: function (value, row, index) {
                //     return '<div style="width:80px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + Table.api.formatter.search.call(this, value, row, index) + '</div>';
                // },
            }
        }
    };
    return Controller;
});
